package cn.relx.controller;

import cn.relx.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "getOrderByIdDefaultFallback") //全局熔断器注解
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
//    @HystrixCommand(fallbackMethod = "getOrderByIdFallbackMethod")  //局部定义熔断器
    @HystrixCommand // 定义全局熔断器的时候注解
    public Object getOrderById(@PathVariable("id") Long id){
        String url = "http://user-server/user/" + id;
        return restTemplate.getForObject(url,User.class);
    }

    //局部熔断器，需要名字一致，返回类型一致，参数一致
    public User getOrderByIdFallbackMethod(@PathVariable("id") Long id){
        return new User(id,"熔断测试!网络异常,请稍后重试!");
    }
    //全局熔断器，方法名字需要一致，返回类型一致
    public Object getOrderByIdDefaultFallback(){
        return new User(-1L,"全局熔断测试！网络异常,请稍后重试!");
    }

}
